package mest.controller.Modeling;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import mest.entity.provider_type;
import mest.entity.save_type;
import mest.service.Modeling.IsaveTypeService;
import mest.util.ResultObj;
import mest.util.mestUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-15 23:27
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/saveType")
public class saveType {
    @Resource
    private IsaveTypeService ipfs;


    @RequestMapping(value = "/insert")
    public String insert(@RequestBody JSONObject json, HttpServletRequest request) {
        save_type record = json.toJavaObject(save_type.class);
        String theirDb = mestUtil.getCompanyDB(request);
        ResultObj obj = ipfs.insertsaveType(record, theirDb);
        return JSON.toJSONString(obj);
    }




    @RequestMapping(value = "/baseSelect", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String baseSelect(int index,int pageSize, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        List<save_type> saveTypePage = ipfs.findSaveTypePage(index, pageSize,theirDb);
        int i = ipfs.selectCount(theirDb);
        Map map  = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",i);
        map.put("data",saveTypePage);
        return JSON.toJSONString(map);
    }


    /**
     * 删除方法
     */
    @RequestMapping("/delModel")
    public Object delModel(@RequestBody JSONObject json, HttpServletRequest request) {
        Map<String,Object> map = json.toJavaObject(Map.class);
        Integer id = (Integer)map.get("id");
        System.out.println("这里的ID是："+id);
        String theirDb = mestUtil.getCompanyDB(request);
        Map<String, Object> modelMap = new HashMap<String, Object>();
        if (ipfs.delModel(id,theirDb) > 0) {
            modelMap.put("erg", "删除成功");
            return JSON.toJSONString(modelMap);
        } else {
            modelMap.put("erg", "不成功");
            return JSON.toJSONString(modelMap);
        }
    }

    @RequestMapping("/queryAll")
    public Object queryAll(Integer index, Integer pageSize,String htmlModel, HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String theirDb = mestUtil.getCompanyDB(request);

        try {
            List<save_type> save_type = ipfs.queryAll(index, pageSize, htmlModel,theirDb);
            if (!ObjectUtils.isEmpty(save_type)) {
                modelMap.put("data", save_type);
                modelMap.put("msg", "success");
                modelMap.put("code", 0);
                modelMap.put("count", ipfs.findCount(htmlModel,theirDb));
            }else {
                modelMap.put("msg", "此对象为空值");
            }

        } catch (Exception e) {
            modelMap.put("msg", "fales");
            modelMap.put("errmsg", e.toString());
        }
        return JSON.toJSONString(modelMap);
    }


    /**
     * 动态更新某一个值或多个值
     * @param json
     * @return
     */
    @RequestMapping("/updateByOne")
    public Object updateByOne(@RequestBody JSONObject json, HttpServletRequest request){
        save_type record = json.toJavaObject(save_type.class);
        String theirDb = mestUtil.getCompanyDB(request);
        System.out.println("==============================================");
        System.out.println("apart_file is "+record.toString()+"; apart_file is "+record);
        Map<String,Object> modelMap = new HashMap<String,Object>();

        if (ipfs.updateByOne(record,theirDb)>0){
            modelMap.put("erg", "删除成功");
            return JSON.toJSONString(modelMap);
        }else {
            modelMap.put("erg", "不成功");
            return JSON.toJSONString(modelMap);
        }
    }



}
